package com.finalcial.controller;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.finalcial.po.LoginMsg;
import com.finalcial.service.ApplicationService;
import com.finalcial.service.DeclarationInformationService;
import com.finalcial.service.WorkService;
import com.finalcial.service.WorkingDetailsService;
import com.finalcial.util.PageUtil;
/**
 * 
 * @author Lenovo
 *
 */
@Controller
@RequestMapping("/work")
public class WorkController {
	
	@Resource
	private WorkService workService;
	@Resource
	private WorkingDetailsService workingDetailsService;
	@Resource
	private DeclarationInformationService declarationInformationService;
	@Resource
	private ApplicationService applicationService;
	
	//待办界面
	@RequestMapping("/myWork")
	public String listWorkMsg(HttpServletRequest request, HttpServletResponse response,HttpSession httpSession) {
		String workStateName = "待办";//来自界面		
		LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
		Integer userId = loginMsg.getUserId();	
		System.out.println(userId);
		List<Map<String, Object>> worklist = null;		
		String pageNow = request.getParameter("pageNow");
		PageUtil page = null;
		Integer totalCount = workService.listWorkMsgCounts(userId, workStateName);//总条数
		if(pageNow != null) {
			page = new PageUtil(totalCount, Integer.parseInt(pageNow));
			worklist = workService.listWorkMsg(userId, workStateName, page.getStartPos(), page.getPageSize());
		}else {
			page = new PageUtil(totalCount, 1);
			worklist = workService.listWorkMsg(userId, workStateName, page.getStartPos(), page.getPageSize());
		}
		/*for(Map<String, Object> map:workdonelist) {
			for(String s :map.keySet()) {
				System.out.println(map.get(s));
			}
		}*/
		request.setAttribute("page", page);
		request.setAttribute("worklist", worklist);
		return "datamanager/personwork/myWork";
	}
	
	@RequestMapping("/approval")
	public ModelAndView listOfficialTrafficApproval(HttpServletResponse response,HttpServletRequest request,HttpSession httpSession) {
		ModelAndView mav = new ModelAndView();
		//显示全部的信息		
		//显示基础信息
		String reimbursementId  = request.getParameter("reimbursementId");
		String reimbursementTypeName = request.getParameter("reimbursementTypeName");
		/*try {
			reimbursementTypeName = new String(reimbursementTypeName.getBytes("iso-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}*/
		String[] arrReimbursementTypeName = reimbursementTypeName.split("-");
		LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
		Integer userId = loginMsg.getUserId();	
		for(int i=0;i<arrReimbursementTypeName.length;i++){
			reimbursementTypeName = arrReimbursementTypeName[0];
		}
		String reimbursementName =reimbursementTypeName; 
		//显示接收人的角色
		List<Map<String, Object>> roleList = applicationService.showRoleNameList();
		//获取基本信息
		List<Map<String, Object>> officeBasicList = workingDetailsService.getEssentialInformation(reimbursementId);
		//获取明细信息(非差旅)
		List<Map<String, Object>> officeDetailList = workingDetailsService.getDetailInformation(reimbursementId, userId);
		//详细信息(差旅)
		List<Map<String, Object>> listDetailInformationBytravel = workingDetailsService.getDetailInformationBytravel(reimbursementId, userId);
		
		//获取内部人信息
		List<Map<String, Object>> officeOtherList = declarationInformationService.getOtherInformation(reimbursementId);
		//解析图片文件
		List<Map<String, Object>> imageList = applicationService.getCheckSheet(reimbursementId);
		mav.addObject("officeBasicList", officeBasicList);
		mav.addObject("officeDetailList", officeDetailList);
		mav.addObject("officeOtherList", officeOtherList);
		mav.addObject("listDetailInformationBytravel", listDetailInformationBytravel);
		mav.addObject("roleList", roleList);
		mav.addObject("imageList", imageList);
		if("国内差旅".equals(reimbursementName)){
			mav.setViewName("datamanager/personwork/domestictravelapproval");
		}else if("公务交通".equals(reimbursementName)){
			mav.setViewName("datamanager/personwork/officialtrafficapproval");
		}else if("物业管理".equals(reimbursementName)){
			mav.setViewName("datamanager/personwork/propertyManagerapproval");
		}else if("员工福利".equals(reimbursementName)){
			mav.setViewName("datamanager/personwork/staffformapproval");
		}
		return mav;
	
	}
	
	
	//多条件查询已办
	@ResponseBody
	@RequestMapping("/chooseWork")
	public List<Map<String, Object>> getWorkDoneMsg(HttpServletResponse response,HttpServletRequest request,HttpSession httpSession){
		String workStateName = "待办";//来自界面
		LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
		Integer userId = loginMsg.getUserId();	
		String reimbursementTypeName = "";
		String userName = request.getParameter("userName");
		String moneyStart = request.getParameter("startMoney");
		String moneyEnd = request.getParameter("endMoney");
		String startTime = request.getParameter("startTime");
		System.out.println(startTime);
		String endTime = request.getParameter("endTime");
		System.out.println(endTime);
		List<Map<String, Object>> chooseWorkList = workService.getWorkMsg(userId, workStateName, reimbursementTypeName, userName, moneyStart, moneyEnd, startTime, endTime);
		return chooseWorkList;
		
	}
	
	
}
